<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 自定义组件 -->
      <page-tools show-before>
        <!-- 左侧数据条数 -->
        <template #before>
          共{{ total }}条数据
        </template>
        <!-- 右侧按钮 -->
        <template #after>
          <el-button size="small" type="danger">简单表头导出</el-button>
          <el-button size="small" type="info">复杂表头导出</el-button>
          <el-button size="small" type="success" @click="$router.push('/import?type=user')">excel导入</el-button>
          <el-button size="small" type="primary" @click="addEmployee">新增员工</el-button>
        </template>
      </page-tools>
      <el-card>
        <el-table border :data="list">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formatter" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry">
            <template v-slot="{row}">
              {{ row.timeOfEntry |formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="" prop="enableState">
            <template v-slot="{row}">
              <el-switch :value="row.enableState===1" />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template slot-scope="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="setRole(row.id)">角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination layout="prev, pager, next" :total="total" :page-size="page.size" @current-change="pageChange" />
        </el-row>
      </el-card>
      <add-employee :dialog-show.sync="dialogShow" />
    </div>
    <set-role ref="setRole" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees.js'
import EmployeesEnum from '@/api/constant/employees.js'// 引入枚举对象
import addEmployee from './components/add-employee.vue'
import SetRole from './components/set-role.vue'
export default {
  name: 'Employees',
  components: { addEmployee, SetRole },
  data() {
    return {
      list: [], // 表格数据
      page: {
        page: 1,
        size: 2
      },
      total: 0,
      dialogShow: false // 弹框显示隐藏
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    // 新增员工
    async addEmployee() {
      this.dialogShow = true
    },
    // 删除员工
    async delEmployee(id) {
      await this.$confirm('是否确认删除')
      await delEmployee(id)
      this.$message.success('删除成功')
      if (this.list.length === 1 && this.page.page > 1) {
        this.page.page -= 1
      }
      this.getEmployeeList()
    },
    // 获取员工列表
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList({
        page: this.page.page,
        size: this.page.size
      })
      // console.log(res)
      this.total = total
      this.list = rows
    },
    // 翻页更新数据
    pageChange(val) {
      this.page.page = val
      this.getEmployeeList()
    },
    // 格式化聘用形式
    formatter(row, column, cellValue, index) {
      const obj = EmployeesEnum.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    // 员工角色设置
    async setRole(id) {
      await this.$refs.setRole.getRoleList()
      await this.$refs.setRole.getuserDetail(id)
      this.$refs.setRole.dialogShow = true
    }
  }

}
</script>

<style>

</style>
